package com.hubing.cloud.app.web.es.query;

public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> {

	private final String fieldName;
	private       Object from;
	private       Object to;

	public RangeQueryBuilder(String fieldName) {
		if (fieldName == null || fieldName.trim().equals("")) {
			throw new IllegalArgumentException("field name is null or empty");
		} else {
			this.fieldName = fieldName;
		}
	}

	public RangeQueryBuilder gte(Object from) {
		this.from = from;
		return this;
	}

	public RangeQueryBuilder lte(Object to) {
		this.to = to;
		return this;
	}

	@Override
	public String toDocument() {
		return String.format(
				"{" + " \"range\": {" + " \"%s\": {" + "     \"gte\": %d," + "     \"lte\": %d" + "   }" + " }}",
				fieldName, from, to);
	}

	@Override
	public String toString() {
		return toDocument();
	}
}
